package org.shangrila.bme.mb;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;

import org.shangrila.app.sys.cfg.AppInitInfo;
import org.shangrila.bme.eo.BmeContract;
import org.shangrila.bme.eo.BmeInvInfo;
import org.shangrila.comm.dao.DbAccess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**  
* <p>Title: InvInfoQueryMB</p>  
* <p>Description: 开票单查询</p>  
* @author 赵功就  
* @date 2018年8月30日  
*/
@Named
@RequestScoped
public class InvInfoQueryMB implements Serializable{

	/** serialVersionUID*/
	private static final long serialVersionUID = -5687633288454928290L;
	
	private final Logger log=LoggerFactory.getLogger(this.getClass());
	
	private BmeInvInfo io=new BmeInvInfo();
	private List<BmeInvInfo> voi=new ArrayList<>();
	
	private List<SelectItem> itemsInvStatus=AppInitInfo.getDropdownList("InvInfoStatus");
	
	@Inject
	private DbAccess db;
	
	@PostConstruct
	public void init() {
		io.setBmeContract(new BmeContract());
	}

	public BmeInvInfo getIo() {
		return io;
	}

	public void setIo(BmeInvInfo io) {
		this.io = io;
	}

	public List<BmeInvInfo> getVoi() {
		return voi;
	}

	public void setVoi(List<BmeInvInfo> voi) {
		this.voi = voi;
	}

	public List<SelectItem> getItemsInvStatus() {
		return itemsInvStatus;
	}

	public void setItemsInvStatus(List<SelectItem> itemsInvStatus) {
		this.itemsInvStatus = itemsInvStatus;
	}
	
	
	
	/**  
	 * <p>Title: queryAction</p>  
	 * <p>Description: 开票信息查询</p>  
	 * @return  
	 */
	public String queryAction() {
		
		StringBuilder sql = new StringBuilder("select bv from BmeInvInfo bv ");
		StringBuilder where = new StringBuilder("where ");
		List<Object> params=new ArrayList<>();
		int i=0;
		if(null!=io.getInvId()&&!"".equals(io.getInvId())) {
			if(i++==0) {
				where.append("bv.invId=?"+i+" ");
			}else {
				where.append("and bv.invId=?"+i+" ");
			}
			params.add(io.getId());
		}
		if(null!=io.getInvDate()&&!(io.getInvDate().toString().equals(""))) {
			if(i++==0) {
				where.append("bv.invDate=?"+i+" ");
			}else {
				where.append("and bv.invDate=?"+i+" ");
			}
			params.add(io.getInvDate());
		}
		if(null!=io.getBmeContract().getId()&&!"".equals(io.getBmeContract().getId())) {
			if(i++==0) {
				where.append("bv.bmeContract=?"+i+" ");
			}else {
				where.append("and bv.bmeContract=?"+i+" ");
			}
			params.add(io.getBmeContract());
		}
		if(null!=io.getContName()&&!"".equals(io.getContName())) {
			if(i++==0) {
				where.append("bv.contName=?"+i+" ");
			}else {
				where.append("and bv.contName=?"+i+" ");
			}
			
			params.add(io.getContName());
		}
		if(null!=io.getStatus()&&!"".equals(io.getStatus())) {
			if(i++==0) {
				where.append("bv.status=?"+i+" ");
			}else {
				where.append("and bv.status=?"+i+" ");
			}
			
			params.add(io.getStatus());
		}
		if(where.length()==6) {
			where=new StringBuilder("");
		}
		
		log.info("invInfo queryAction:"+sql.append(where.toString()));
		if("".equals(where.toString())) {
			voi=db.findBySql(sql.toString());
		}else {
			voi=db.findBySql(sql.toString(), params.toArray());
		}
		
		
		return null;
	}
}
